package zf.display.tools
{
	import flash.display.BitmapData;
	import flash.display.DisplayObject;
	import flash.display.Loader;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.HTTPStatusEvent;
	import flash.events.IOErrorEvent;
	import flash.events.ProgressEvent;
	import flash.net.URLRequest;
	import flash.system.LoaderContext;
	
	/**
	 * 加载完成后初始化事件 
	 * @eventType flash.events.Event.INIT
	 */
	[Bindable]
	[Event(name="init", type="flash.events.Event")]
	
	/**
	 * 加载进程事件 
	 * @eventType flash.events.ProgressEvent.PROGRESS
	 */
	[Bindable]
	[Event(name="progress", type="flash.events.ProgressEvent")]
	
	/**
	 * 加载错误事件 
	 * @eventType flash.events.IOErrorEvent.IO_ERROR
	 */
	[Bindable]
	[Event(name="io_error", type="flash.events.IOErrorEvent")]
	
	/**
	 * 加载时https状态事件 
	 * @eventType flash.events.HTTPStatusEvent.HTTP_STATUS
	 */
	[Bindable]
	[Event(name="http_status", type="flash.events.HTTPStatusEvent")]
	/**
	 * 显示对象加载 
	 * @param uri
	 * @author feng.zhang
	 * @langversion 3.0 
	 * @playerversion Flash 10 
	 */
	public class DisplayLoader extends Sprite
	{
		private var _url:String;
		private var _content:DisplayObject;
		private var _contentType:String;
		
		private var loader:Loader;
		
		private var _process:Number=0;
		
		public var data:Object;
		
		public static const CONTENT_TYPE_SWF:String="swf";
		public static const CONTENT_TYPE_JPEG:String="jpeg";
		public static const CONTENT_TYPE_GIF:String="gif";
		public static const CONTENT_TYPE_PNG:String="png";
		/**
		 * 显示对象加载 
		 * @param uri
		 * @author feng.zhang
		 * @langversion 3.0 
		 * @playerversion Flash 10 
		 */
		public function DisplayLoader(uri:String=null)
		{
			super();
			
			loader=new Loader();
			loader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS,onLoadProgressHander);
			loader.contentLoaderInfo.addEventListener(Event.INIT,onLoadInitHander);
			loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR,onLoadErrorHander);
			loader.contentLoaderInfo.addEventListener(HTTPStatusEvent.HTTP_STATUS,onHttpsStatusHander);
			
			this.addChild(loader);
			this.addEventListener(Event.REMOVED_FROM_STAGE,onRemoveHander);
			if(uri)
			{
				_url=uri;
				load();
			}
		}		

		/**
		 * 加载进程 0-1 之间取值
		 * @return 
		 * 
		 */
		public function get process():Number
		{
			return _process;
		}

		/**
		 * 加载完成后获得该显示对象 
		 * @return DisplayObject类型
		 * 
		 */
		public function get content():DisplayObject
		{
			return _content;
		}
		
		/**
		 * 显示对象的类型 
		 * @return 
		 * 
		 */
		public function get contentType():String
		{
			return _contentType;
		}

		/**
		 * 显示对象所在的地址 
		 * @return 
		 * 
		 */
		public function get url():String
		{
			return _url;
		}
		
		/**
		 * 是否图片 
		 * @return 
		 * 
		 */
		public function isImage():Boolean
		{
			return _contentType!=null && _contentType!="swf";
		}
		
		/**
		 * 开始加载 
		 * @param uri
		 * 
		 */
		public function load(uri:String=null,context:LoaderContext=null):void
		{
			if(uri)
			{
				_url=uri;
			}
			loader.load(new URLRequest(_url),context);
		}
		
		private function onLoadProgressHander(e:ProgressEvent):void
		{
			_process=e.bytesLoaded/e.bytesTotal;
			this.dispatchEvent(e);
		}
		private function onLoadInitHander(e:Event):void
		{
			getContentType();
			_process=1;
			_content=loader.content;
			this.dispatchEvent(e);
		}
		private function onLoadErrorHander(e:IOErrorEvent):void
		{
			_process=0;
			throw new Error(e.text,e.errorID);
			this.dispatchEvent(e);
		}
		private function onHttpsStatusHander(e:HTTPStatusEvent):void
		{
			this.dispatchEvent(e);
		}
		
		private function getContentType():void
		{
			if(loader==null || loader.contentLoaderInfo==null)
			{
				_contentType=null;
			}
			switch(loader.contentLoaderInfo.contentType)
			{
				case "application/x-shockwave-flash":
					_contentType="swf";
					break;
				case "image/jpeg":
					_contentType="jpeg";
					break;
				case "image/gif":
					_contentType="gif";
					break;
				case "image/png":
					_contentType="png";
					break;

			}
		}
		private function onRemoveHander(e:Event=null):void
		{
			if(loader)
			{
				if(loader.contentLoaderInfo.hasEventListener(ProgressEvent.PROGRESS))
				{
					loader.contentLoaderInfo.removeEventListener(ProgressEvent.PROGRESS,onLoadProgressHander);
				}
				if(loader.contentLoaderInfo.hasEventListener(Event.INIT))
				{
					loader.contentLoaderInfo.removeEventListener(Event.INIT,onLoadInitHander);
				}
				if(loader.contentLoaderInfo.hasEventListener(IOErrorEvent.IO_ERROR))
				{
					loader.contentLoaderInfo.removeEventListener(IOErrorEvent.IO_ERROR,onLoadErrorHander);
				}
				if(loader.contentLoaderInfo.hasEventListener(HTTPStatusEvent.HTTP_STATUS))
				{
					loader.contentLoaderInfo.removeEventListener(HTTPStatusEvent.HTTP_STATUS,onHttpsStatusHander);
				}
				_content=null;
				/*if(_contentType=="swf")
				{
					loader.unloadAndStop();
				}else
				{
					loader.unload();
				}*/
				loader.unload();
			}
			loader=null;
		}
		
		/**
		 * 释放 
		 * 
		 */
		public function dispose():void
		{
			onRemoveHander();
		}
	}
}